<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      @submit.native.prevent>

      <el-form-item label="日期">
        <el-date-picker
          v-model="tempDateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          class="small-date-picker"
          @change="handleDateChange" />
      </el-form-item>
      <el-form-item label="商户ID" prop="accountId">
        <el-input v-model="queryParams.accountId" placeholder="请输入商户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderItemInfoList">
      <el-table-column label="商户ID" align="center" prop="accountId" />
      <el-table-column label="商户信息" align="center">
        <template slot-scope="scope">
          {{ scope.row.accountName }}
          <span v-if="scope.row.priceType === '2'" style="color: red;">CPS/{{ scope.row.typeRate }}</span>
          <span
            :style="{ color: scope.row.infoType === 'API' ? 'red' : scope.row.infoType === 'CRM' ? 'blue' : 'green' }">
            [{{ scope.row.infoType }}]
          </span>
        </template>
      </el-table-column>
      <el-table-column label="商务信息" align="center" prop="ownerName" width="120">
        <template slot-scope="scope">
          {{ scope.row.ownerName }} [ {{ scope.row.ownerId }} ]
        </template>
      </el-table-column>
      <el-table-column label="推送数量" align="center" prop="number" />
      <el-table-column label="总消耗" align="center" prop="totalChangeAmount">
        <template slot-scope="{ row }">
          <span style="color:blue">{{ row.totalChangeAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-edit" @click="redirectToInfo(scope.row)">明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listOrderItemInfo1 } from "@/api/merchant/orderItemInfo";

export default {
  name: "OrderItemInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户匹配订单子项信息表格数据
      orderItemInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        dateInfo: null,
        infoId: null,
        infoType: null,
        taskId: null,
        profitAmount: null,
        uid: null,
        appCode: null,
        appName: null,
        channelCode: null,
        channelName: null,
        status: null,
        pushId: null,
        remarkInfo: null,
        approveFlag: null,
        protocolUrl: null,
        approveUrl: null,
        accountId: null,
        startDateInfo: null,
        endDateInfo: null,
      },
      tempDateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.$route.query
    }
    this.getList();
    this.setDefaultDateRange();
  },
  watch: {
    $route() {
      this.getList()
    }
  },
  methods: {
    setDefaultDateRange() {
      const today = moment().format('YYYY-MM-DD');
      this.queryParams.startDateInfo = today;
      this.queryParams.endDateInfo = today;
      this.tempDateRange = [today, today];
    },

    handleDateChange(dateRange) {
      if (dateRange && dateRange.length === 2) {
        this.queryParams.startDateInfo = dateRange[0];
        this.queryParams.endDateInfo = dateRange[1];
      } else {
        this.queryParams.startDateInfo = null;
        this.queryParams.endDateInfo = null;
      }
    },
    /** 查询商户匹配订单子项信息列表 */
    getList() {
      this.loading = true;

      const params = { ...this.queryParams };

      listOrderItemInfo1(params)
        .then(response => {
          if (response && response.rows) {
            this.orderItemInfoList = response.rows.slice(
              (this.queryParams.pageNum - 1) * this.queryParams.pageSize,
              this.queryParams.pageNum * this.queryParams.pageSize
            );
            this.total = response.total;
          } else {
            this.orderItemInfoList = [];
            this.total = 0;
          }
          this.loading = false;
        })
        .catch(error => {
          console.error("Error fetching data:", error);
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /**数据跳转页面 */
    redirectToInfo(row) {
      if (row.infoType === 'CRM') {
        this.$router.push(`/merchant/crmpushInfo?infoId=${row.accountId}`);
      } else {
        this.$router.push(`/merchant/pushInfo?infoId=${row.accountId}&status=SUCCESS&dateInfo=${row.dateInfo}&channelName=${this.queryParams.channelName}`);
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        dateInfo: null,
        infoId: null,
        infoType: null,
        taskId: null,
        profitAmount: null,
        uid: null,
        appCode: null,
        appName: null,
        channelCode: null,
        channelName: null,
        status: null,
        pushId: null,
        remarkInfo: null,
        approveFlag: null,
        protocolUrl: null,
        approveUrl: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dateRange = [];
      this.handleQuery();
    },
    resetForm(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].resetFields();
      }
    }
  }
};
</script>
